/**
 * Created by LiuJie on 2017/10/16.
 * 用户管理
 */
var userInfoTable;
var viewAddPage_url = "/editUserInfo";
var UserInfo = function () {

    var viewUserList_url = "/dataManage";
    var saveUserData_url = "/editUserInfo/saveData";
    var updateData_url = "/editUserInfo/updateData";
    function initTable() {
        userInfoTable = new BootstrapTable($('#tb_departments'),{
            url: '/dataManage/queryUserList',
            sidePagination: "server",
            toolbar: '#toolbar',
            singleSelect:false,
            height:500,
            queryParams:function (params) {
                var _params = {
                    codeName:$("#codeName").val(),
                    deptManager:$("#deptManager").val(),
                    phone:$("#phone").val(),
                };
                if($("#kindId").val()){
                    _params["kindId"] =$("#kindId").val();
                }
                return $.extend(params,_params);
            },
            columns: [{
                checkbox: true
            }, {
                field: 'name',
                title: '用户名'
            }, {
                field: 'parentName',
                title: '所属上级'
            }, {
                field: 'phone',
                title: '联系电话'
            },{
                field: 'id',
                title: '操作',
                formatter:function (value,row,index) {
                    var opHtml = "";
                    var isEdit = AuthUtils.getOpAuthById("14_35");
                    var isDel = AuthUtils.getOpAuthById("14_36");
                    if(isEdit){
                        opHtml+= "<button type=\"button\" class=\"btn  btn-theme\" style=\"margin-bottom: 3px;margin-top: 3px;margin-right: 3px;padding: 3px 6px;\" onclick='editUserView("+row.id+")'>修 改</button>";
                    }
                    if(isDel){
                        opHtml+= "<button type=\"button\" class=\"btn btn-danger\"  style=\"margin-bottom: 3px;margin-top: 3px;padding: 3px 6px;\" onclick='deleteRow("+row.id+")'>删 除</button>";
                    }
                    return opHtml
                }
            } ]
        });
        userInfoTable.init();
    }

    /**
     * 初始化用户管理列表页面事件
     */
    function initEvent() {
        $("#btn_add").on("click",function () {
            $.link(viewAddPage_url);
        });
        /**
         * 重置
         */
        $("#resetForm").on("click",function () {
            $(this).parents(".content-search").find("input").val("");
        });

        $("#dosearch").on("click",function () {
            var params = {
                codeName:$("#codeName").val(),
                deptManager:$("#deptManager").val(),
                phone:$("#phone").val(),
            };
            if($("#kindId").val()){
                params["kindId"] =$("#kindId").val();
            }
            userInfoTable.refresh({query:params});
        });
        $(".selectUser").on("click",function () {
            $("#myTreeDialog").modal("show");
        });
    }

    /**
     * 初始化用户管理编辑页面事件
     */
    function initEditUserInfoEvent() {
        $("#submitForm").on("click",function () {
            if($("#parentName").val() == ""){
                $.messageExt.alert("系统提示","所属上级是必填项！","warning");
                return false
            }
            if($("#name").val() == ""){
                $.messageExt.alert("系统提示","用户名称是必填项！","warning");
                return false
            }
            if($("#id").val()){
                update();
            }else{
                saveData();
            }
        });
        $("#back-view").click(function () {
            $.link(viewUserList_url);
        });
        $("#userInfoForm").validForm([
            {
                name:"parentName",
                validate:{
                    type:"require",
                    message:"所属上级是必填项"
                }
            },
            {
                name:"name",
                validate:[
                    {
                        type:"require",
                        message:"用户名称为必填项"
                    },
                    {
                        type:"length",
                        value:[1,20],
                        message:"用户名称不能超过20个字符"
                    }
                ]
            }
        ])
    }
    function saveData() {
        $.ajax({
            url:saveUserData_url,
            type:"POST",
            data:{
                parentId:$("#parentId").val(),
                parentName:$("#parentName").val(),
                name:$("#name").val(),
                deptManager:$("#deptManager").val(),
                phone:$("#phone").val(),
            },
            dataType:"json"
        }).done(function (result) {
            if(result.isSuccess === true){
                $.messageExt.alert("系统提示","用户添加成功！","success");
                // $("#baseTerminalIdTwo").val($("#baseTerminalId").val());
                // $("#baseTerminalIdTree").val($("#baseTerminalId").val());
                $("#parentId").val("");
                $("#name").val("");
                $("#deptManager").val("");
                $("#phone").val("");
                $("#parentName").val("");
                $("#id").val("");
            }
        })
    }
    function update() {
        $.ajax({
            url:updateData_url,
            type:"POST",
            data:{
                id:$("#id").val(),
                parentId:$("#parentId").val(),
                name:$("#name").val(),
                deptManager:$("#deptManager").val(),
                phone:$("#phone").val(),
                parentName:$("#parentName").val(),
            },
            dataType:"json"
        }).done(function (result) {
            if(result.isSuccess === true){
                $.messageExt.alert("系统提示","用户更新成功！","success");
                // $("#baseTerminalIdTwo").val($("#baseTerminalId").val());
                // $("#baseTerminalIdTree").val($("#baseTerminalId").val());
                $("#parentId").val("");
                $("#name").val("");
                $("#deptManager").val("");
                $("#phone").val("");
                $("#parentName").val("");
                $("#id").val("");
            }
        })
    }
    
    return{
        initUserPage:function () {
            initTable();
            initEvent();
        },
        initEditUserPage:function () {
            initEditUserInfoEvent();
        }
    }
}();
function editUserView(id) {
    $.link(viewAddPage_url + "?id="+id);
}


